﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SunRiseDemo.Controls
{
    /// <summary>
    /// SunriseControl.xaml 的交互逻辑
    /// </summary>
    public partial class SunriseControl : UserControl
    {
        double MeasuredWidth = 0.0;
        double MeasuredHeight = 0.0;

        // 太阳大布局的宽高，划线用
        double SunLayoutWidth = 0;
        double SunLayoutHeight = 0;

        public SunriseControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 测量子元素
        /// 调用 Measure() 方法，会触发此函数
        /// </summary>
        /// <param name="constraint"></param>
        /// <returns></returns>
        protected override Size MeasureOverride(Size constraint)
        {
            Size baseSize = base.MeasureOverride(constraint);
            // 获取控件的高度和宽度
            if (constraint.Width > 0)
                MeasuredWidth = constraint.Width;
            if (constraint.Height > 0)
                MeasuredHeight = constraint.Height;

            return baseSize;
        }

        /// <summary>
        /// 排列元素
        /// </summary>
        /// <param name="arrangeBounds"></param>
        /// <returns></returns>
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            return base.ArrangeOverride(arrangeBounds);
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            SunLayoutWidth = SunLightLayout.ActualWidth;
            SunLayoutHeight = SunLightLayout.ActualWidth;
            DrawAllSunLines();
        }


        #region 画图
        private void DrawAllSunLines()
        {
            double centerX = SunLayoutWidth / 2;
            double centerY = SunLayoutHeight / 2;

            var gridBrush = new SolidColorBrush { Color = Colors.White };
            for (int i = 0; i < 8; i++)
            {
                Line line = new Line
                {
                    X1 = centerX,
                    Y1 = 20,
                    X2 = centerX,
                    Y2 = 30,
                    Stroke = gridBrush,
                    StrokeThickness = 3,
                };
                line.RenderTransform = new RotateTransform(i * 45, centerX, centerY);
                SunLightLayout.Children.Add(line);
            }
        }
        #endregion
    }
}
